<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // let arr1 = [1, 2, 3, 4, 5]; // arr2 = [1,2,3,4,5]   arr1[0] = 99  arr2 = [1,2,3,4,5]

        // let arr2 = [];
        // for (let i = 0; i < arr1.length; i++) {
        //     arr2.push(arr1[i]);
        // }

        // console.log('arr1:',arr1);
        // console.log('arr2:',arr2);

        // arr1[0] = 99;

        // console.log('arr1:',arr1);
        // console.log('arr2:',arr2);

        // let arr1 = [1, 2, 3, [4, 5, 6, [7, 8, 9, ['a', 'b']]]]; // -> 扁平化成 [1,2,3,4,5,6,7,8,9]

        // 1. 转字符串，再转回来
        // let arr2 = arr1.join(',').split(',').map(item => parseInt(item));
        // console.log('arr1', arr1);
        // console.log('arr2', arr2);

        // 2. 递归
        // let arr2 = [];
        // function flat(arr) {
        //     for (let item of arr) { // item 
        //         if (Array.isArray(item)) { // item 就是数组
        //             flat(item);
        //         } else { // 不是数组
        //             arr2.push(item);
        //         }
        //     }
        // }
        // flat(arr1);
        // console.log(arr2);


        let arr1 = [1, 2, 3, [4, 5, 6, [7, 8, 9]]];
        let arr2 = [];   // arr2 -> [1, 2, 3, [4, 5, 6, [7, 8, 9]]];

        function clone(arr, newArr) {
            for (let i = 0; i < arr.length; i++) {
                if (Array.isArray(arr[i])) { // 数组
                    let temp = [];
                    newArr[i] = temp;
                    clone(arr[i], temp);
                } else {
                    newArr.push(arr[i]);
                }
            }
        }
        clone(arr1, arr2);

        console.log(arr2);
        arr1[3][3] = 99;
        console.log(arr2);




    </script>
</body>

</html>